<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 
            状态转移方程：
            dp[i]=Math.max(dp[i-1],dp[i-2]+nums[i])
            
            */
            var rob = function (nums) {
                //长度为1单独考虑...
                if (nums.length == 1) return nums[0]
                let nums1 = nums.slice(1)
                let nums2 = nums.slice(0, nums.length - 1)

                return Math.max(getRob(nums1), getRob(nums2))
                function getRob(nums) {
                    let dp = new Array(nums.length + 1)
                    dp[0] = nums[0]
                    dp[1] = Math.max(nums[0], nums[1])
                    for (let i = 2; i < nums.length; i++) {
                        dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i])
                    }
                    return dp[dp.length - 2]
                }
            }
        </script>
    </body>
</html>
